package com.summer_administration.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.summer_administration.exception.BusinessException;
import com.summer_administration.mapper.GroupeMapper;
import com.summer_administration.mapper.PublicityMapper;
import com.summer_administration.pojo.PageBean;
import com.summer_administration.pojo.Publicity;
import com.summer_administration.service.PublicityService;
import com.summer_administration.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class PublicityServiceImpl implements PublicityService {
    @Autowired
    private PublicityMapper publicityMapper;

    @Autowired
    private GroupeMapper groupeMapper;

    @Override
    public PageBean<Publicity> list(Integer pageNum, Integer pageSize) {
        PageBean<Publicity> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);

        Map<String, Object> userInfo = ThreadLocalUtil.get();
        Integer userId = (Integer) userInfo.get("id");

        Page<Publicity> page = (Page<Publicity>) publicityMapper.list(userId);
        //pb.setTotal(page.getTotal());
        pb.setItems(page.getResult());
        return pb;
    }

    @Override
    @Transactional
    public void update(Publicity publicity) {
        // 验证数据是否存在
        Publicity existing = publicityMapper.getById(publicity.getId());
        if (existing == null) {
            throw new BusinessException("宣传材料不存在");
        }

        // 新增一个根剧小组名字查询小组id
        String groupeName = publicity.getGroupName();
        String groupId = groupeMapper.findGroupIdByGroupName(groupeName);
        //publicity.setGroupId(groupId);

        // 更新操作只能修改指定字段
        existing.setCommune(publicity.getCommune());
        existing.setPicture(publicity.getPicture());
        existing.setLink(publicity.getLink());
        existing.setUpdateTime(LocalDateTime.now());
        existing.setGroupId(groupId);

        publicityMapper.update(existing);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        if (publicityMapper.getById(id) == null) {
            throw new BusinessException("宣传材料不存在");
        }
        publicityMapper.delete(id);
    }

    @Override
    @Transactional
    public void add(Publicity publicity) {
        Map<String, Object> userInfo = ThreadLocalUtil.get();
        Integer userId = (Integer) userInfo.get("id");

        publicity.setCreateUser(userId);
        publicity.setCreateTime(LocalDateTime.now());
        publicity.setUpdateTime(LocalDateTime.now());

        // 新增一个根剧小组名字查询小组id
        String groupeName = publicity.getGroupName();
        String groupId = groupeMapper.findGroupIdByGroupName(groupeName);
        publicity.setGroupId(groupId);

        publicityMapper.add(publicity);
    }
}